/*
package com.audaque.springboot.foshanupload.web.querydsldemo.controller;



import com.audaque.springboot.foshanupload.web.querydsldemo.service.DemoService;
import com.audaque.springboot.foshanupload.web.querydslmodedemo.model.dto.DemoDto;
import com.audaque.springboot.foshanupload.web.querydslmodedemo.model.entity.DemoEntity;
import com.audaque.springboot.foshanupload.web.querydslmodedemo.model.entity.QDemoEntity;
import com.audaque.springboot.foshanupload.web.querydslmodedemo.model.entity.QUserEntity;
import com.audaque.springboot.foshanupload.web.querydslmodedemo.model.entity.UserEntity;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.Tuple;
import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.Projections;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.querydsl.binding.QuerydslPredicate;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

*/
/**
 * @Author: ONESTAR
 * @QQ群: 530311074
 * @URL: https://onestar.newstar.net.cn/
 *//*

@RequestMapping("/demo")
@RestController
public class DemoController {

    @Autowired
    private JPAQueryFactory jpaQueryFactory;



    @Autowired
    private DemoService demoService;





    */
/**
     * @QuerydslPredicate可以将http请求的参数转换为Predicat,但是不兼容@RequestBody,只能用get在url传参
     * 时间格式需要加指定注解  @DateTimeFormat(pattern = "yyyy-MM-dd")
     * @param predicate
     * @return
     *//*

    @RequestMapping(value = "/seach", method = RequestMethod.GET)
    public Iterable<DemoEntity> getUsers(
            @QuerydslPredicate(root = DemoEntity.class) Predicate predicate) {
        Iterable<DemoEntity> list = demoService.findAll(predicate);
        return list;
    }

    @RequestMapping(value = "/pages", method = RequestMethod.GET)
    public Map<String,Object> pages(
            @QuerydslPredicate(root = DemoEntity.class) Predicate predicate,
            @RequestParam(defaultValue = "1") Integer pageNo,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        return  demoService.page(predicate,pageNo,pageSize);
    }


    @GetMapping("contation")
    public List<DemoEntity> contation() {
        QDemoEntity demoEntity = QDemoEntity.demoEntity;
        //单表获取年龄为10的结果集
        List<DemoEntity> ageList = jpaQueryFactory.selectFrom(demoEntity).where(demoEntity.age.eq(10)).fetch();
        return ageList;

    }


    @GetMapping("like")
    public List<DemoEntity> like() {
        QDemoEntity demoEntity = QDemoEntity.demoEntity;
        //模糊查询并排序
        List<DemoEntity> ageList = jpaQueryFactory.selectFrom(demoEntity).where(demoEntity.name.like("%大%")).orderBy(demoEntity.age.desc()).fetch();
        return ageList;
    }
    */
/**
     * 区间用法（between）
     *//*

    @GetMapping("between")
    public List<DemoEntity> between() throws ParseException {
        QDemoEntity demoEntity = QDemoEntity.demoEntity;
        //between 区间的用法：单表获取开始时间是XX-XX 区间的用户
        String time = "2019-07-22 00:00:00";
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date= df.parse(time);
        Timestamp nousedate = new Timestamp(date.getTime());

        String time1 = "2019-07-23 00:00:00";
        Date date1= df.parse(time1);
        Timestamp nousedate1 = new Timestamp(date1.getTime());

        List<DemoEntity> beginTimeList = jpaQueryFactory.selectFrom(demoEntity).where(demoEntity.createTime.between(nousedate, nousedate1)).fetch();
        return beginTimeList;
    }
    */
/**
     * 分页查询
     *//*

    @GetMapping("limit")
    public List<DemoEntity> limit() {
        Integer pageNo=1;
        Integer pageSize=10;
        QDemoEntity demoEntity = QDemoEntity.demoEntity;
        HashMap<String, Object> result = new HashMap<>();
        BooleanExpression booleaBuilder = demoEntity.name.like("%大%");
        long count = jpaQueryFactory.select(demoEntity.count()).from(demoEntity).
                where(booleaBuilder).fetchOne();
        List<DemoEntity> list = jpaQueryFactory.select(demoEntity).
                from(demoEntity).
                where(booleaBuilder).
                orderBy(demoEntity.age.desc()).
                offset((pageNo-1)*pageSize).
                limit(pageSize).
                fetch();
        result.put("content",list);
        result.put("total",count);
        result.put("pageNo",pageNo);
        result.put("pageSize",pageSize);
        return list;
    }
    */
/**
     * in查询
     *//*

    @GetMapping("in")
    public List<DemoEntity> in() {
        QDemoEntity demoEntity = QDemoEntity.demoEntity;
        //in 的用法：单表获取年龄是10,20的用户
        List<Integer> ageLists = new ArrayList<>();
        ageLists.add(10);
        ageLists.add(20);
        List<DemoEntity> list = jpaQueryFactory.selectFrom(demoEntity).where(demoEntity.age.in(ageLists)).fetch();
        return list;
    }
    */
/**
     * 查询将用户id，名称，年龄拼接的结果
     *//*

    @GetMapping("concat")
    public List<String> concat() {
        QDemoEntity demoEntity = QDemoEntity.demoEntity;
        //聚合函数-concat()的使用：单表查询将用户id，名称，年龄拼接的结果
        List<String> concatList = jpaQueryFactory.select(demoEntity.id.stringValue()
                        .concat(demoEntity.name).concat(demoEntity.age.stringValue()))
                .from(demoEntity).fetch();
        return concatList;
    }
    */
/**
     * 聚合函数group by的用法：
     *//*

    @SneakyThrows
    @GetMapping("group")
    public List<Map<String,Object>>  group() {
        QDemoEntity demoEntity = QDemoEntity.demoEntity;
        List<Map<String,Object>> tupleJPAQuery = jpaQueryFactory.select(demoEntity.age, demoEntity.count().as("count"))
                .from(demoEntity).groupBy(demoEntity.age)
                .fetch().stream().map(x->{
                    Map<String,Object> resultMap = new HashMap<>();
                    resultMap.put("age",x.get(0,QDemoEntity.class));
                    resultMap.put("count",x.get(1,QDemoEntity.class));
                    return resultMap;
                }).collect(Collectors.toList());

        return tupleJPAQuery;
    }
    */
/**
     * 多条件查询：
     *//*

    @GetMapping("booleanBuilder")
    public List<DemoEntity> booleanBuilder() {
        QDemoEntity demoEntity = QDemoEntity.demoEntity;
        //多条件处理
        BooleanBuilder booleanBuilder = new BooleanBuilder();
        booleanBuilder.and(demoEntity.age.eq(10));
        booleanBuilder.and(demoEntity.name.contains("小"));
        List<DemoEntity> mostlist = jpaQueryFactory.selectFrom(demoEntity).where(booleanBuilder).fetch();
        return mostlist;
    }
    */
/**
     * 多表查询：
     *//*

    @GetMapping("leftjoin")
    public List<DemoDto> leftjoin() {
        QDemoEntity demoEntity = QDemoEntity.demoEntity;
        QUserEntity userEntity = QUserEntity.userEntity;
        List<Tuple> tupleList = jpaQueryFactory.select(demoEntity.age,userEntity.height,userEntity.name)
                .from(demoEntity).leftJoin(userEntity).on(demoEntity.userId.eq(userEntity.id))
                .orderBy(userEntity.age.desc()).fetch();
        System.out.println("tupleList的结果集:" + tupleList);
        List<Map<String, Object>> resultList = tupleList.stream().map(x -> {
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("nameDept", x.get(0, DemoEntity.class));
            resultMap.put("nameUser", x.get(0, UserEntity.class));
            resultMap.put("demo-age", x.get(demoEntity.age));
            resultMap.put("user-height", x.get(userEntity.height));
            resultMap.put("user-name", x.get(userEntity.name));
            return resultMap;
        }).collect(Collectors.toList());
        System.out.println("resultList的结果集:" + resultList);

        //封装一下结果集
        List<DemoDto> list = jpaQueryFactory.select(
                        Projections.bean(DemoDto.class,
                                demoEntity.age,userEntity.height,userEntity.name))
                .from(demoEntity).leftJoin(userEntity).on(demoEntity.userId.eq(userEntity.id))
                .orderBy(userEntity.age.desc()).fetch();
        System.out.println("list的结果集:" + list);
        return list;
    }


}
*/
